import moment from 'moment';

const getDate = (datestr) => {
    return new Date(datestr);
}
//获取开始日期及结束日期之间的日期
export const getDateBetweenDates = (start, end) => {
    let startTime = getDate(start);
    let endTime = getDate(end);
    let result: any = [];
    while ((endTime.getTime() - startTime.getTime()) >= 0) {
        let year = startTime.getFullYear();
        let month = startTime.getMonth().toString().length == 1 ? "0" + (startTime.getMonth()+1).toString() : startTime.getMonth()+1;
        let day = startTime.getDate().toString().length == 1 ? "0" + startTime.getDate() : startTime.getDate();
        result.push(year + "-" + month + "-" + day);
        startTime.setDate(startTime.getDate() + 1);
    }
    return result;
}
let f = 'YYYY-MM-DD';
let f1 = 'YYYY-MM-DD HH:MM';
//获取当前日期所在周的开始日期和结束日期
export const getWeekStartAndEnd = (date = '2018-05-30',weekstart?) => {
    let m;
    if (typeof date == 'string') {
        m = moment(date, f);
    } else {
        m = date;
    }
    let weekOfday = parseInt(m.format('E'));//计算今天是这周第几天 
    weekOfday = weekOfday == 7 ? 1 : (weekOfday + 1);
    if(weekstart&&weekstart=='2'){//周一作为一周开始日期
        weekOfday=weekOfday-1;
    }
    let sunday = moment(m).subtract(weekOfday - 1, 'days').format(f);//周日日期  
    let saturday = moment(m).add(7 - weekOfday, 'days').format(f);//周六日期 
    return { sunday, saturday };
}

//是否当周日期
export const isInWeek = (currentDate, movedate, objDate,weekstart?) => {
    let { sunday, saturday } = getWeekStartAndEnd(currentDate,weekstart);
    if (movedate == '1') {
        sunday = moment(sunday, f).add(7, 'days').format(f);
        saturday = moment(saturday, f).add(7, 'days').format(f);
    } else if (movedate == '-1') {
        sunday = moment(sunday, f).subtract(7, 'days').format(f);
        saturday = moment(saturday, f).subtract(7, 'days').format(f);
    }
    let s = new Date(sunday).getTime();
    let e = new Date(saturday).getTime();
    let o = new Date(objDate).getTime();
    if (o >= s && o <= e) {
        return true;
    } else {
        return false;
    }
}

export const addDay = (currentDate, day) => {
    let m;
    if (typeof currentDate == 'string') {
        m = moment(currentDate, f);
    } else {
        m = currentDate;
    }
   let currentday= moment(m, f).add(day, 'days').format(f);
   return currentday;
}

//获取当天的开始时刻及结束时刻
export const getHourBetweenDates = (currentDate, movedate, startDate, startTime, endDate, endTime,dspunit) => {
    currentDate = currentDate.split(' ')[0];
    let hour: any = [];
    let current = moment(currentDate, f1);
    if (movedate == '1') {
        current.add(1, 'days').format(f1);
    } else if (movedate == '-1') {
        current.subtract(1, 'days').format(f1);
    }
    let start: any = new Date(Date.parse((startDate + " " + startTime).replace(/-/g, "/")));
    let start_h = startTime.split(':')[0];//开始时间小时
    let start_m = startTime.split(':')[1];//开始时间分钟
    if (start_m != '00') {
        let  dspunittime= dspunit==2?parseInt(start_m)<30?":00":":30":":00";
        start = new Date(Date.parse((startDate + " " + start_h + dspunittime).replace(/-/g, "/")));
    }
    let end: any = new Date(Date.parse((endDate + " " + endTime).replace(/-/g, "/")));
    for (let i = 0; i < 24; i=i+(1/dspunit)) {
        let  dspunittime= Math.floor(i)==i?":00":":30";
        let m;
        if (i < 10) {
            m = new Date(Date.parse((current.format(f) + " 0" + Math.floor(i) + dspunittime).replace(/-/g, "/")));
        } else {
            m = new Date(Date.parse((current.format(f) + " " + Math.floor(i) + dspunittime).replace(/-/g, "/")));
        }
        if (start.getTime() <= m.getTime() && m.getTime() < end.getTime()) {
            hour.push(i);
        }
    }
    return hour;
}
